package com.yupi.yuso.designpattern.adapter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yuso.designpattern.register.DataSourceRegister;
import com.yupi.yuso.model.dto.post.PostQueryRequest;
import com.yupi.yuso.model.dto.search.SearchRequest;
import com.yupi.yuso.model.enums.SearchTypeEnum;
import com.yupi.yuso.model.vo.PostVO;
import com.yupi.yuso.service.PostService;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

/**
 * @author vvv
 * @date 2023-12-21 21 59
 * discription
 */
@Component
public class PostDataSourceAdapter implements DataSourceTarget<PostVO> {

    @Resource
    private PostService postService;

    @PostConstruct
    public void registerInstance() {
        DataSourceRegister.SEARCH_MAP.put(SearchTypeEnum.POST.getValue(), this);
    }

    @Override
    public Page<PostVO> doSearch(SearchRequest searchRequest) {
        PostQueryRequest postQueryRequest
                = BeanUtil.copyProperties(searchRequest, PostQueryRequest.class);
        Page<PostVO> postVOPage = postService.listPostVOByPage(postQueryRequest, null);
        return postVOPage;
    }
}
